package com.example.buildtable.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaJoinQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.buildtable.pojo.Columns;
import com.example.buildtable.pojo.Newsreport;
import com.example.buildtable.pojo.ProductInbound;
import com.example.buildtable.util.ProductInboundQuery;
import com.example.common.util.R;
import com.example.buildtable.pojo.ProductInbound;
import com.example.buildtable.service.ProductInboundService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Tag(name = "产品入库")
@RequestMapping(value = "/productInbound/")
public class ProductInboundController {
    @Autowired
    private ProductInboundService productInboundService;
    
    @Operation(summary = "查询所有产品入库数据")
    @GetMapping(value = "/getAllProductInbound")
    public R getAllProductInbound() {
        List<ProductInbound> productInboundList = productInboundService.list();
        return R.ok("查询所有产品入库数据成功").data("productInboundList", productInboundList);
    }

    @Operation(summary = "查询所有产品入库模糊查询带分页")
    @PostMapping(value = "/getProductInboundPage/{page}/{limit}")
    public R getProductInboundPage(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @RequestBody(required = false) ProductInboundQuery productInboundQuery) {
        LambdaJoinQueryWrapper<ProductInbound> wrapper = new LambdaJoinQueryWrapper<>(ProductInbound.class);
        //接收条件
        Integer productInboundId = productInboundQuery.getProductInboudId();
        String productInboundName = productInboundQuery.getProductInboundName();
        Date createTimeStart = productInboundQuery.getCreateTime();
        Date createTimeEnd = productInboundQuery.getCreateTime();
        wrapper.like(productInboundId != null && !productInboundId.equals(-1), ProductInbound::getProductInboundId, productInboundId);
        wrapper.like(productInboundName != null && !productInboundName.equals(""), ProductInbound::getProductInboundName, productInboundName);
        wrapper.like(createTimeStart != null && !productInboundId.equals(""), ProductInbound::getCreateTime, createTimeStart);
        wrapper.like(createTimeEnd != null && !productInboundId.equals(""), ProductInbound::getCreateTime, createTimeEnd);
        Page<ProductInbound> productInboundPage = new Page<>(page, limit);
        productInboundService.page(productInboundPage, wrapper);
        //每页显示的记录
        List<ProductInbound> productInboundList = productInboundPage.getRecords();
        //共多少条记录
        Long total = productInboundPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", productInboundList);
        return R.ok("查询所有产品入库模糊查询带分页成功").data("map", map);
    }
    
    @Operation(summary = "增加产品入库")
    @PostMapping(value = "/addProductInbound")
    public R addProductInbound(ProductInbound productInbound) {
        boolean flag = productInboundService.save(productInbound);
        if (flag) {
            return R.ok("增加产品入库成功");
        } else {
            return R.ok("增加产品入库失败");
        }
    }

    @Operation(summary = "删除产品入库")
    @GetMapping(value = "/deleteProductInbound/{productInboundId}")
    public R deleteProductInbound(@PathVariable("productInboundId") Integer productInboundId) {
        boolean flag = productInboundService.removeById(productInboundId);
        if (flag) {
            return R.ok("删除产品入库成功");
        } else {
            return R.ok("删除产品入库失败");
        }
    }

    @Operation(summary = "按照产品入库查看")
    @GetMapping(value = "/getProductInboundById/{productInboundId}")
    public R getProductInboundById(@PathVariable("productInboundId") Integer productInboundId) {
        ProductInbound productInbound = productInboundService.getById(productInboundId);
        if (productInbound != null) {
            return R.ok("按照产品入库查看成功").data("productInbound", productInbound);
        } else {
            return R.error("按照产品入库查看失败");
        }
    }

    @Operation(summary = "修改产品入库")
    @PostMapping(value = "/updateProductInbound")
    public R updateProductInbound(ProductInbound productInbound) {
        boolean flag = productInboundService.updateById(productInbound);
        if (flag) {
            return R.ok("修改产品入库成功");
        } else {
            return R.ok("修改产品入库失败");
        }
    }
}